import os
import time
from tkinter import *
import tkinter.messagebox
import threading
import requests

startBrowserCommand = 'node "google_developer.js"'
start_option = 'start'
gmail_option = 'gmail'
pay_option = 'pay'
profile_option = 'profile'
pay2_option = 'pay2'
step_option = 'step'
developer_option = 'developer'
upload_option = 'upload'
proxy_option = 'proxy'
threadId = 0
i = 0

changeLinks = ['http://node-us-25.astroproxy.com:10449/api/changeIP?apiToken=bf0fb895fa005b8d',
               'http://node-us-28.astroproxy.com:10425/api/changeIP?apiToken=bf0fb895fa005b8d',
               'http://node-us-17.astroproxy.com:10489/api/changeIP?apiToken=bf0fb895fa005b8d',
               'http://node-us-60.astroproxy.com:10481/api/changeIP?apiToken=bf0fb895fa005b8d',
               'http://node-us-74.astroproxy.com:10599/api/changeIP?apiToken=bf0fb895fa005b8d']


def changeIP(brow_item):
    get = requests.get(changeLinks[brow_item.i])
    print(get)
    pass


class BrowItem:
    def __init__(self):
        global i
        self.id = int(round(time.time() * 1000))
        self.i = i
        self.L1 = Label(top, text="序号")
        self.L1.grid(row=i, column=0)

        self.E1 = Entry(top, bd=5)
        self.E1.grid(row=i, column=1)

        self.B = tkinter.Button(top, text="启动浏览器", command=lambda: Open(start_option, self))
        self.B.grid(row=i, column=2)

        self.B2 = tkinter.Button(top, text="登录", command=lambda: Open(gmail_option, self))
        self.B2.grid(row=i, column=3)

        # self.B3 = tkinter.Button(top, text="点击验证", command=lambda: Open(pay_option, self))
        # self.B3.grid(row=i, column=4)

        self.B3 = tkinter.Button(top, text="填写密码", command=lambda: Open(profile_option, self))
        self.B3.grid(row=i, column=5)

        self.B4 = tkinter.Button(top, text="填写两步", command=lambda: Open(pay2_option, self))
        self.B4.grid(row=i, column=6)

        self.B5 = tkinter.Button(top, text="开发者", command=lambda: Open(step_option, self))
        self.B5.grid(row=i, column=7)

        # self.B6 = tkinter.Button(top, text="更换IP", command=lambda: changeIP(self))
        # self.B6.grid(row=i, column=8)

        i += 1

    def get_entry(self):
        return self.E1.get()

    def get_id(self):
        return self.id

    def set_id(self):
        self.id = int(round(time.time() * 1000))


class myThread(threading.Thread):
    def __init__(self, threadID, name, counter, option, entry, id1):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.option = option
        self.entry = entry
        self.id = id1

    def run(self):
        os.system(startBrowserCommand + ' ' + self.option + ' ' + self.entry + ' ' + str(self.id))


def Open(option, brow_item):
    global threadId
    global start_option
    if option == start_option:
        brow_item.set_id()
    threadId += 1
    print(brow_item.get_id())
    thread1 = myThread(threadId, "Thread-" + str(threadId), threadId, option, brow_item.get_entry(), brow_item.get_id())
    thread1.start()


def Gmail(entry):
    global threadId
    threadId += 1
    thread1 = myThread(threadId, "Thread-" + str(threadId), threadId, entry)
    thread1.start()


if __name__ == '__main__':
    top = Tk()
    # hwnd = pywintypes.HANDLE(int(top.frame(), 16))
    # print(top.frame())
    # system = os.system('H:\\911\\3.35\\ProxyTool\\ProxyAPI -changeproxy/US -zip=92626 -proxyport=5000 -hwnd=' + str(top.winfo_id()))
    # print(system)
    for i in range(0, 5):
        BrowItem()
    # top.overrideredirect(True)
    # top.attributes("-alpha", 1)
    top.wm_attributes('-topmost', 1)
    top.mainloop()
